package org.aptech.hdax.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.aptech.hdax.dao.SysRoleDao;
import org.aptech.hdax.pojo.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleDao sysRoleDao;

    @RequestMapping("/search")
    @ResponseBody
    public List<SysRole> search() throws Exception{
        return sysRoleDao.getAll();
    }

    @RequestMapping("/assignPers")
    @ResponseBody
    public Map<String,Object> assginPers(Integer roleId, String perIds) throws Exception{
        Map<String,Object> map = new HashMap<>();

        sysRoleDao.deletePersByRoleId(roleId);
        sysRoleDao.addRolePers(roleId,perIds.split(","));

        return map;
    }

    @RequestMapping("/rolePers")
    @ResponseBody
    public List<Long> rolePers(Integer roleId) throws Exception{
        return sysRoleDao.getPerIdsByRoleId(roleId);
    }

    @RequestMapping("/index")
    public String index() throws Exception {
        return  "system/role/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit, SysRole condition) throws Exception{
        Map<String,Object> map = new HashMap<>();

        Page<SysRole> pager = PageHelper.startPage(page,limit);

        map.put("code",0);
        map.put("msg",null);
        map.put("data",sysRoleDao.getList(condition));
        map.put("count",pager.getTotal());

        return map;
    }
}
